//
//  BDShopCommentViewController.m
//  BadBoy
//
//  Created by wen on 2017/3/24.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopCommentViewController.h"
#import "BDCommondTableViewCell.h"
//#import "BDShopEditCommondViewController.h"
#import "BDUserModel.h"
#import "BDShopCommonModel.h"
@interface BDShopCommentViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BDShopCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = ASLocalizedString(@"全部评论");
    [[NSUserDefaults standardUserDefaults] setObject:@NO forKey:@"needRefreshCommend"];
    
    [self bindRAC];
    [self requestCommon];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"needRefreshCommend"] boolValue] == YES) {
        [self.tableView.mj_header beginRefreshing];
        [[NSUserDefaults standardUserDefaults] setObject:@NO forKey:@"needRefreshCommend"];
    }
}

- (void)requestCommon{
    //请求评论
    NSString *headerVale = [BDUserModel getHeaderValidation];
    NSString *url = [KBDBaseURL stringByAppendingString:[NSString stringWithFormat:@"shop/%ld/comments?page=%@&size=%@", self.detailModel.shop_id, self.startPage, self.pageSize]];
    RACCommand *command = [BDNetworkingServer yy_GET:url parameters:nil validation:headerVale automaticProcessingError:NO];
    RACSignal *signal = [command execute:nil];
    
    [signal subscribeNext:^(id  _Nullable x) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.datasource addObjectsFromArray:[BDShopCommonModel mj_objectArrayWithKeyValuesArray:x[@"list"]]];
        [self.tableView reloadData];
        self.isEnd = [x[@"is_end"] boolValue];
        if (self.isEnd == YES) {
            self.tableView.mj_footer = nil;
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 40)];
            label.textColor = [UIColor colorWithHexString:@"#767676"];
            label.font = [UIFont systemFontOfSize:14];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor clearColor];
            label.text = ASLocalizedString(@"以上是所有内容");
            self.tableView.tableFooterView = label;
        }else{
            self.startPage = x[@"next_page"];
        }
    }];
    
    [signal subscribeError:^(NSError * _Nullable error) {
        // BDLog(ASLocalizedString(@"获取评论列表失败"));
        [self showErrorViewWithTapBlock:^{
            [self requestCommon];
        }];
    }];
}


- (void)bindRAC{

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [BDCommondTableViewCell getCellHeightWithModel:self.datasource[indexPath.row]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDCommondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCommonCell" forIndexPath:indexPath];
//    // BDLog(ASLocalizedString(@"当前的indexpath:%@  当前的数据类型:%@"), indexPath, NSStringFromClass([self.datasource[indexPath.row] class]));
    
    [cell configeWithModel:self.datasource[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.sectionFooterHeight = 10;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[BDCommondTableViewCell class] forCellReuseIdentifier:@"detailCommonCell"];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self requestCommon];
        }];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.datasource removeAllObjects];
            self.startPage = @"1";
            [self requestCommon];
        }];
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
